package com.mc.book.pojo;

import com.mc.book.enums.ResponseEnum;

import java.io.Serializable;

// TODO 后续可以优化为枚举抛出等异常捕获的统一处理
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 8082009123967326657L;

    private Integer code;

    private String message;

    private T data;

    public static <T> Result<T> raw(){
        return success(ResponseEnum.ram.getCode(), null,null);
    }

    public static <T> Result<T> raw(T data){
        return success(ResponseEnum.ram.getCode(), null,data);
    }

    public static <T> Result<T> success(Integer code,String message,T data){
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> success(Integer code,String message){
        return success(code,message,null);
    }


    public static <T> Result<T> success(T data){
        return success(ResponseEnum.success.getCode(),ResponseEnum.success.getMessage(),data);
    }

    public static <T> Result<T> success(){
        return success(ResponseEnum.success.getCode(),ResponseEnum.success.getMessage());
    }

    public static <T> Result<T> fail(Integer code,String message,T data){
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        result.setData(data);
        return result;
    }
    public static <T> Result<T> fail(Integer code,String message){
        return fail(code,message,null);
    }

    public static <T> Result<T> fail(){
        return fail(ResponseEnum.fail.getCode(),ResponseEnum.fail.getMessage(),null);
    }


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
